<?php

use Doraemon\model\Article;
use Doraemon\model\ArticlePhoto;
use Doraemon\service\Article as sArticle;
use Doraemon\tools\Filter;
use Doraemon\tools\Log;
use Doraemon\tools\StringUtil;

class ResetRemarkAction extends ActionPlugin
{
    const IS_TRACING = true;

    /**
     * 迁移hhzhome队列 - 整屋摘要
     */
    public function main()
    {
        Log::info('ResetRemark操作接收到的订阅消息', $this->messageBody);

        if (!$this->messageBody['database']) throw new \Exception('未获取到数据库');
        if (!$this->messageBody['table']) throw new \Exception('未获取到数据表');
        if (!$this->messageBody['data']) throw new \Exception('未接收到数据');

        $sArticleId = $this->messageBody['data']['aid'];

        $aHeadInfo = sArticle::getArticleHeadInfo($sArticleId);

        $sContentText = '';
        if ($aHeadInfo && is_array($aHeadInfo)) {
            $sContentText .= $aHeadInfo['description'];
        }

        $aArticleSpaceData = sArticle::getArticleSpace($sArticleId, $aHeadInfo['space_sort_test'] ?? '');
        if (!empty($aArticleSpaceData)) {
            foreach ($aArticleSpaceData as $aArticleSpace) {
                if ($aArticleSpace['show_pics'] && is_array($aArticleSpace['show_pics'])) {
                    foreach ($aArticleSpace['show_pics'] as $aSpacePhotoItem) {
                        if (StringUtil::strlen($sContentText) < 200) {
                            $sContentText .= $aSpacePhotoItem['remark'];
                        }
                    }
                }
            }
        }

        $sContentText = Filter::removeEmoji($sContentText);
        $sRemark = Filter::getRemark($sContentText);

        Article::updateArticle($sArticleId, ['remark' => $sRemark]);

        $this->setSuccess();
    }
}
